// TOWN SCRIPT
//    Town 30: Grendel's Lair

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documenation. States you write
// yourself should be numbered from 10-100.

begintownscript;

variables;

short i,j,k,r1,choice;

body;

beginstate INIT_STATE;
		set_state_continue(37);
// This state called whenever this town is entered.
break;

beginstate EXIT_STATE;
// Always called when the town is left.
break;

beginstate START_STATE;
// This state is called every turn the party is in this town.
break;

beginstate 10;
	reset_dialog_preset_options(2);
	choice = run_dialog(0);
	if (choice == 2) {
		set_state_continue(15);
		}
break;


beginstate 15;
	swap_floor(24,18,170,27);
	swap_floor(24,19,170,23);
	swap_floor(24,20,170,31);
	flip_terrain(5,5);
	flip_terrain(7,6);
	flip_terrain(7,7);
break;

beginstate 22;
	if (is_combat()) {
	block_entry(1); 
	end();
	}
	teleport_party(5,30,1);
break;

beginstate 23;
	if (get_flag(30,1) == 250)
		end();
	reset_dialog();
	add_dialog_str(0,"You reach the central island of the lake. The rumors about the treasure weren't false: tremendous stacks of shining valuables are piled all over. Come to think of it, there's only one thing wrong in all that you've heard about this place.",0);
	add_dialog_str(1,"There is no monster.",0);
	add_dialog_str(2,"Perhaps some searching might reveal more.",0);
	add_dialog_choice(0,"OK.");
	choice = run_dialog(1);
	set_flag(30,1,250);
	set_state_continue(30);
break;

beginstate 24;
	reset_dialog();
	add_dialog_str(0,"Something here doesn't look quite like treasure. You bend over to inspect it. It's apparently a journal or diary. The most recent entry reads, in a spidery script:",0);
	add_dialog_str(1,"_The swamplings bring tributes every day. I do not know their purpose. I feel that something is eating away at my spirit, a monstrous presence. Perhaps it is the inherent soul of this fen.",0);
	add_dialog_str(2," Grendel is immortal, they say. I know now what they meant. I shall not last long. I can only hope to resist it for as long as I may. But it will win. Who is to fight with a god?_",0);
	add_dialog_str(3,"A thin line trails off to end the journal.",0);
	add_dialog_choice(0,"OK.");
	choice = run_dialog(1);
	set_state_continue(27);
break;

beginstate 25;
	if (character_in_party(1702) >= 0) 
		set_state_continue(26);
	set_state_continue(42);
break;

beginstate 26;
	if (get_flag(30,6) == 250)
	set_state_continue(42);
	message_dialog("Fernow shudders. _That's Dowan's writing. He must be here._","");
		set_flag(30,6,250);
break;

beginstate 27;
	set_flag(30,3,1);
	set_state_continue(25);
break;

beginstate 28;
	if (get_flag(30,3) >= 1)
		set_state_continue(29);
break;

beginstate 29;
	if (get_flag(30,4) == 250)
		end();
	reset_dialog();
	add_dialog_str(0,"An arm crosses the path ahead, stopping you in your tracks. Another arm, with a splash of water. Then the entire form of the monster clambers up into your view.",0);
	add_dialog_str(1,"It is vaguely humanoid. The leather that it appears to wear might have been human-make. Its disgusting visage, however, is anything but. Bloated, corrupted, rotting, it stands there, staring at you. You do not dare return its gaze.",0);
	add_dialog_str(2,"It does not move. It simply waits there, dripping water and slime.",0);
	add_dialog_choice(0,"Attack.");
	add_dialog_choice(1,"Approach.");
	choice = run_dialog(1);
	if (choice != 1)
		set_flag(30,4,250);
	if (choice == 1)
		set_state_continue(31);
	if (choice == 2)
		set_state_continue(32);
break;

beginstate 30;
	set_flag(97,4,1);
	
	if (character_in_party(2802) >= 0) {
	reset_dialog();
	add_dialog_str(0,"With a shock, you realize that Ephon has also disappeared.",0);
	add_dialog_choice(0,"OK.");
	choice = run_dialog(1);
	remove_char_from_party(2802);
	}

break;

beginstate 31;
	message_dialog("As soon as you draw your weapon, a berserk rage seizes it. Gurgling madly, it attacks, heedless of any fear or pain.","");
	activate_hidden_group(1);
break;

beginstate 32;
	reset_dialog();
	add_dialog_str(0,"It stares pitifully at you. Slowly, painfully, it raises up a withered hand, as though gesturing for you to stop. Gurgling sounds come out of its throat, but nothing that you could possibly comprehend. You can only look on in horror.",0);
	add_dialog_str(1,"Suddenly, the silence is pierced by the ring of a bow. A shaft pierces the creature's shoulder from behind. Fresh red blood drips out from the wound. With a growl, it turns to face its attacker.",0);
	add_dialog_str(2,"On the shore stands Ephon, a grim look on his face. A long bow is strung with another arrow. He takes his aim, and fires again.",0);
	add_dialog_choice(0,"OK.");
	choice = run_dialog(1);
	set_state_continue(33);
break;

beginstate 33;
	if (character_in_party(1702) >= 0) 
		set_state_continue(34);
	set_state_continue(35);
break;

beginstate 34;
	reset_dialog();
	add_dialog_str(0,"The arrow does not reach its destination. A whizzing stone deflects it off course, and it falls harmlessly into the water. Ephon checks a shout of surprise, staring straight at you. Then you realize he's not looking at you at all.",0);
	add_dialog_str(1,"Another stone slices through the stale air. Ephon rolls to his left and avoids it. The raging Grendel hurtles straight at him. He takes up another arrow, but a stone knocks it out of his hand. Then the monster is upon him. You turn away.",0);
	add_dialog_str(2,"_Leave him be,_ Fernow says in a calm voice. You realize that something has changed about her. _Leave them all be._",0);
	add_dialog_str(3,"Leading you away from the grisly scene, she walks towards the exit of the cave.",0);
	add_dialog_choice(0,"OK.");
	choice = run_dialog(1);
	set_state_continue(36);
break;

beginstate 35;
	reset_dialog();
	add_dialog_str(0,"The arrow strikes home. With a glottal yell, the monster tumbles into the water.",0);
	add_dialog_str(1,"_Escaped again,_ Ephon mutters angrily. _It'll be back. It always comes back. Just go. I'll deal with it when it comes again._",0);
	add_dialog_str(2,"Seeing your hesitation, he shoots you an impatient look. _This is my task, my duty. I thank you for helping me get here, but your job is done here. Now go._",0);
	add_dialog_str(3,"He slips behind a large stone, vanishing from your line of sight.",0);
	add_dialog_choice(0,"OK.");
	choice = run_dialog(1);
break;

beginstate 36;
	set_flag(30,9,1);
	set_state_continue(40);
break;

beginstate 37;
	if (get_flag(30,9) >= 1)
		set_state_continue(38);
	set_state_continue(39);
break;

beginstate 38;
	message_dialog("There is nothing else for you to do in this cave. It's probably best that you leave.","");
	block_entry(1);
break;

beginstate 39;
	if (get_flag(30,8) == 250)
		end();
	reset_dialog();
	add_dialog_str(0,"The cavern is ominously silent. Dominated by a large, motionless lake in the middle, not a single moving creature appears to be in here. The sound of your footsteps echo off the walls eerily.",0);
	add_dialog_choice(0,"OK.");
	choice = run_dialog(1);
	set_flag(30,8,250);
break;

beginstate 40;
	teleport_party(45,24,1);
break;

beginstate 42;
	message_dialog("Searching through the journal some more, you find a short prayer with potent properties. You memorize Return Life.","");
	i = 0;
	while (i < 6) {
		if (char_ok(i)) {
			if (get_spell_level(i,1,16) <= 2)
					change_spell_level(i,1,16,1);
			else 
				print_named_str(i,"cannot learn any more of this spell.");
			}
		i = i + 1;
}
break;

	

